#!/bin/bash
#  This script takes 3 parameters.  
# 1 - The name of the detector (ADProsilica, etc.)
# 2 - The version number (R2-0, etc.)
# 3 - The EPICS architecture (linux-x86_64, etc.)

echo "Parameter 1 = " $1
echo "Parameter 2 = " $2
echo "Parameter 3 = " $3

find . -name 'auto_settings.sav_*' -exec rm -fv {} \;
find . -name 'auto_settings.savB*' -exec rm -fv {} \;
find . -name 'core.*' -exec rm -fv {} \;
find . -name '*.exe.*' -exec rm -fv {} \;

AREA_DETECTOR=areaDetector-3-2
ASYN=asyn-4-33
AUTOSAVE=autosave-5-9
BUSY=busy-1-7
CALC=calc-3-7
DEVIOCSTATS=devIocStats-3-1-15
SSCAN=sscan-2-11-1
BASE_TOP=/usr/local/epics
BASE=base-7.0.1
SUPPORT=/home/epics/support
DESTINATION=/home/epics/public_html/pub/$1/$1_Prebuilt_$AREA_DETECTOR_$2_$3
# NOTE: This cd is needed so that wildcards work, even though the tar command has --directory
cd ..
tar czf $DESTINATION.tgz \
--ignore-failed-read \
--exclude '.svn*' \
--exclude 'O.*'  \
--directory $BASE_TOP \
$BASE/bin/$3 \
$BASE/lib/$3 \
--directory $SUPPORT \
$AREA_DETECTOR/ADCore/db \
$AREA_DETECTOR/ADCore/documentation \
$AREA_DETECTOR/ADCore/ADApp/Db \
$AREA_DETECTOR/ADCore/ADApp/op \
$AREA_DETECTOR/ADCore/iocBoot \
$AREA_DETECTOR/ADViewers/ImageJ \
$AREA_DETECTOR/ADCore/bin/$3 \
$AREA_DETECTOR/ADCore/lib/$3 \
$AREA_DETECTOR/ADSupport/bin/$3 \
$AREA_DETECTOR/ADSupport/lib/$3 \
$AREA_DETECTOR/$1/db \
$AREA_DETECTOR/$1/bin/$3 \
$AREA_DETECTOR/$1/lib/$3 \
$AREA_DETECTOR/$1/*/Db \
$AREA_DETECTOR/$1/*/op \
$AREA_DETECTOR/$1/documentation \
$AREA_DETECTOR/$1/iocs/*IOC/bin/$3 \
$AREA_DETECTOR/$1/iocs/*IOC/lib/$3 \
$AREA_DETECTOR/$1/iocs/*IOC/dbd \
$AREA_DETECTOR/$1/iocs/*IOC/iocBoot \
$ASYN/opi \
$ASYN/db \
$ASYN/bin/$3 \
$ASYN/lib/$3 \
$AUTOSAVE/asApp/Db \
$AUTOSAVE/asApp/op \
$AUTOSAVE/bin/$3 \
$AUTOSAVE/lib/$3 \
$BUSY/busyApp/Db \
$BUSY/busyApp/op \
$BUSY/bin/$3 \
$BUSY/lib/$3 \
$CALC/calcApp/Db \
$CALC/calcApp/op \
$CALC/bin/$3 \
$CALC/lib/$3 \
$DEVIOCSTATS/db \
$DEVIOCSTATS/op/adl \
$DEVIOCSTATS/bin/$3 \
$DEVIOCSTATS/lib/$3 \
$SSCAN/sscanApp/Db \
$SSCAN/sscanApp/op \
$SSCAN/bin/$3 \
$SSCAN/lib/$3

rm -f $DESTINATION.zip
cd $BASE_TOP
zip -r -q $DESTINATION.zip   \
$BASE/bin/$3
cd $SUPPORT
zip -r -q --grow $DESTINATION.zip   \
$AREA_DETECTOR/ADCore/db \
$AREA_DETECTOR/ADCore/documentation \
$AREA_DETECTOR/ADCore/ADApp/Db \
$AREA_DETECTOR/ADCore/ADApp/op \
$AREA_DETECTOR/ADCore/iocBoot \
$AREA_DETECTOR/ADViewers/ImageJ \
$AREA_DETECTOR/ADCore/bin/$3 \
$AREA_DETECTOR/ADCore/lib/$3 \
$AREA_DETECTOR/ADSupport/bin/$3 \
$AREA_DETECTOR/ADSupport/lib/$3 \
$AREA_DETECTOR/$1/db \
$AREA_DETECTOR/$1/bin/$3 \
$AREA_DETECTOR/$1/lib/$3 \
$AREA_DETECTOR/$1/*/Db \
$AREA_DETECTOR/$1/*/op \
$AREA_DETECTOR/$1/documentation \
$AREA_DETECTOR/$1/iocs/*IOC/bin/$3 \
$AREA_DETECTOR/$1/iocs/*IOC/lib/$3 \
$AREA_DETECTOR/$1/iocs/*IOC/dbd \
$AREA_DETECTOR/$1/iocs/*IOC/iocBoot \
$ASYN/opi \
$ASYN/db \
$ASYN/bin/$3 \
$ASYN/lib/$3 \
$AUTOSAVE/asApp/Db \
$AUTOSAVE/asApp/op \
$AUTOSAVE/bin/$3 \
$AUTOSAVE/lib/$3 \
$BUSY/busyApp/Db \
$BUSY/busyApp/op \
$BUSY/bin/$3 \
$BUSY/lib/$3 \
$CALC/calcApp/Db \
$CALC/calcApp/op \
$CALC/bin/$3 \
$CALC/lib/$3 \
$DEVIOCSTATS/db \
$DEVIOCSTATS/op/adl \
$DEVIOCSTATS/bin/$3 \
$DEVIOCSTATS/lib/$3 \
$SSCAN/sscanApp/Db \
$SSCAN/sscanApp/op \
$SSCAN/bin/$3 \
$SSCAN/lib/$3 \
--exclude '*.svn*' --exclude '*/O.*' 
